import store from "@/store";

function checkPermission(el, binding) {
  const { value } = binding;
  // 获取用户全部按钮权限
  const userPermission = store.getters && store.getters.permission;

  if (value === undefined) {
    return;
  }
  if (value) {
    // 判断用户是否有该权限
    const hasPermission = userPermission.indexOf(value) != -1 ? true : false;

    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  } else {
    throw new Error(`v-permission="按钮标识名称"`);
  }
}

export default {
  inserted(el, binding) {
    checkPermission(el, binding);
  },
  update(el, binding) {
    checkPermission(el, binding);
  },
};
